package com.wanghua.mall.user.interceptor;

import com.wanghua.common.vo.UserResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wanghua
 * @create 2021-09-07 21:58
 */
@Component
public class UserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<UserResponseVo> threadLocal = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //  放此路径过来 /order/order/getOrderStatus/{orderNo}

        UserResponseVo loginUser = (UserResponseVo) request.getSession().getAttribute("loginUser");
        System.out.println(loginUser);
        if (loginUser == null) {
            //未登录
            request.getSession().setAttribute("msg", "请先登录");
            response.sendRedirect("http://auth.mall.com/login.html");
            return false;
        } else {
            threadLocal.set(loginUser);
            return true;
        }
    }
}
